package com.ihd.ihardware.view.enter.model;

import android.databinding.ObservableArrayList;

import com.google.gson.JsonObject;
import com.ihd.ihardware.R;
import com.ihd.ihardware.api.HttpClient;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.HomeDevice;
import com.ihd.ihardware.pojo.UpdateRes;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.utils.FileUtils;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class HomeRepository extends BaseRepository {

    public void update() {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("platformType", "1");
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().getLatestVersion(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<UpdateRes>(AppConstans.MUPGRADE) {
                }));
    }

    public void download(UpdateRes updateInfo) {
        Disposable disposable = HttpClient.getService().downloadapk(updateInfo.getData().getVersionUrl())
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribeWith(new DisposableObserver<ResponseBody>() {
                    @Override
                    public void onNext(ResponseBody responseBody) {
                        if (FileUtils.writeResponseBodyToDisk(responseBody, AppConstans.UPDATE_PATH)) {
                            LiveEventBus.get().with(AppConstans.M_DOWNLOAD_APK).post(updateInfo);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        addDisposable(disposable);
    }

    public void getLifeHealthList(ObservableArrayList<HomeDevice> mDT, DeviceListRes.DataBean.ListBean tzcddl) {
        mDT.clear();
        List<HomeDevice> mList = new ArrayList<>();
        mList.add(new HomeDevice(R.drawable.text_index_tzc, "体脂秤", "科学管理好身材", R.drawable.bg_46a5f9_9362f9, tzcddl != null ? 1 : 0));
        mList.add(new HomeDevice(R.drawable.text_index_yyc, "营养秤", "称一称饮食更健康", R.drawable.bg_4cffffff, 2));
        mDT.addAll(mList);
    }

    public void getHealthWorkList(ObservableArrayList<HomeDevice> mDT, DeviceListRes.DataBean.ListBean hbcddl) {
        mDT.clear();
        List<HomeDevice> mList = new ArrayList<>();
        mList.add(new HomeDevice(R.drawable.text_index_hbc_none, "滑板车", "智能滑板车出行更方便", R.drawable.bg_1acbff_006cff, hbcddl != null ? 1 : 0));
        mList.add(new HomeDevice(R.drawable.text_index_zh_none, "手环", "手环在手运动不愁", R.drawable.bg_4cffffff, 2));
        mDT.addAll(mList);
    }

    public void getLifeUseList(ObservableArrayList<HomeDevice> mDT, List<DeviceListRes.DataBean.ListBean> lysddl) {
        mDT.clear();
        List<HomeDevice> mList = new ArrayList<>();
        mList.add(new HomeDevice(R.drawable.text_index_lys, "蓝牙锁", "一个专属私人空间", R.drawable.bg_9362f9_46a5f9, lysddl.size() > 0 ? 1 : 0));
        mDT.addAll(mList);
    }

    public void unbind(String id) {
        addDisposable(HttpClient.getService().unbind(id)
                .flatMap(new Function<EmptyRes, Publisher<DeviceListRes>>() {
                    @Override
                    public Publisher<DeviceListRes> apply(EmptyRes emptyRes) throws Exception {
                        if (emptyRes.getCode() == 0)
                            return HttpClient.getService().deviceList(1, 100);
                        else {
                            return Flowable.error(new Exception(emptyRes.getMessage()));
                        }
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<DeviceListRes>(AppConstans.UNBIND) {
                }));
    }
}
